package com.huazhi.wzsnw.service.impl;

import com.huazhi.wzsnw.mapper.ArchivesMapper;
import com.huazhi.wzsnw.model.Archives;
import com.huazhi.wzsnw.service.ArchivesService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class ArchivesServiceImpl implements ArchivesService {
    @Autowired
    private ArchivesMapper archivesMapper;

    @Override
    public Archives selectByPrimaryKey(String tid){return archivesMapper.selectByPrimaryKey(tid);}
    @Override
    public int insert (Archives record){return archivesMapper.insert(record);}
    @Override
    public List<String> getArchivesList(String key, Archives archives){return archivesMapper.getArchivesList(key,archives);}
    @Override
    public int delete(Archives record){return archivesMapper.deleteByPrimaryKey(record.getTid());}
    @Override
    public int deletes(List<Archives> records){
        if (!records.isEmpty()) {
            for (Archives archives : records) {
                delete(archives);
            }
        }
        return 0;
    }
    @Override
    public int update(Archives record){return archivesMapper.updateByPrimaryKey(record);}
    @Override
    public Archives findByTid(String tid){return archivesMapper.selectByPrimaryKey(tid);}
    @Override
    public JSONObject getArchivesListOne(HttpServletRequest request){
        JSONObject jsonObject = new JSONObject();
        int pageSize = Integer.parseInt(request.getParameter("limit"));
        int pages = Integer.parseInt(request.getParameter("page"));
        Integer pageIndex = (pages - 1) * pageSize;
        List<String> list = getSearchArchivesList(request);
        if(list.isEmpty()){
            jsonObject.put("data", false);
            jsonObject.put("count", 0);
            jsonObject.put("msg", "");
        }else{
            List<Archives> circuitList = getListArchivesByListTid(list,pageIndex,pageSize);
            jsonObject.put("data", circuitList);
            jsonObject.put("count", list.size());
            jsonObject.put("msg", "");
        }
        jsonObject.put("code", 0);
        return jsonObject;
    }
    @Override
    public List<String> getSearchArchivesList(HttpServletRequest request){
        String search = request.getParameter("key");
        String selectdata = request.getParameter("selectdata");
        List<String> list = new ArrayList<>();
        if(selectdata != null && !selectdata.isEmpty()){
            String[] ids = selectdata.split(",");
            if(ids.length >0 ){
                list.addAll(Arrays.asList(ids));
            }
        }else {
            Archives archives =new Archives();
            List<String> params = new ArrayList<>();
            if ("".equals(search) || search == null) {
                params.add("");
            } else if (search.contains(" ")) {
                params = Arrays.asList(search.split(" "));
            } else if (!search.contains(" ")) {
                params.add(search);
            }
            boolean firstSearch = true;
            for (String oneParam : params) {
                if (firstSearch) {
                    list = getArchivesList(oneParam,archives);
                    firstSearch = false;
                } else {
                    List<String> middleList = getArchivesList(oneParam,archives);
                    list.retainAll(middleList);
                }
            }
        }
        return list;
    }
    @Override
    public List<String> selectArchivesList(String key,Archives archives){return archivesMapper.getArchivesList(key,archives);}
    @Override
    public List<Archives> getListArchivesByListTid(List<String> stringList, Integer pageIndex, Integer pageSize){return archivesMapper.selectListArchivesByListTid(stringList,pageIndex,pageSize);}

}
